﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Tpl.Ef.Pe;

namespace Tpl.EfMigration
{
    public class LibraryDbContextBuilder : IDesignTimeDbContextFactory<LibraryDbContext>
    {
        public LibraryDbContext CreateDbContext(string[] args)
        {
            // 此处写死，简单一点，读取配置文件比较复杂，只是为了迁移数据库
            string connStr = "Host=127.0.0.1;Port=5432;Database=library;User ID=postgres;Password=sa*123;Pooling=true;MinPoolSize=10;MaxPoolSize=50;";
            DbContextOptionsBuilder<LibraryDbContext> builder = new();
            builder.UseNpgsql(connStr, b => b.MigrationsAssembly("Tpl.EfMigration"));

            var entityInfo = new Tpl.Domain.EFEntityInfo();

            LibraryDbContext ctx = new LibraryDbContext(builder.Options,entityInfo);

            return ctx;
        }
    }
}